<template>
  <div class="modal" tabindex="-1" role="dialog" aria-hidden="true" :id="id">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">
            <slot name="title"> 价格修改 </slot>
          </h5>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-1">
              <label class="col-3 col-form-label text-end">
                <slot name="priceTitle"> 金额 </slot>
              </label>
              <div class="col-9">
                <input
                  type="text"
                  class="form-control"
                  v-model.number="price"
                />
              </div>
            </div>
            <div class="row">
              <label class="col-3 col-form-label text-end">
                <slot name="remarkTitle"> 备注 </slot>
              </label>
              <div class="col-9">
                <input type="text" class="form-control" v-model.trim="remark" />
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
           <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";

export default {
  props: {
    id: {
      type: String,
      default: "modalPrompt",
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      price: 0,
      remark: "",

      myModal: null
    };
  },
  methods: {
    ok: function () {
      if (this.price <= 0 || this.remark === null || this.remark.length === 0) {
        return;
      }

      this.hideModal();

      this.resolve({ price: this.price, remark: this.remark });
    },
    modal: function (price, remark) {
      this.showModal();

      this.price = price;
      this.remark = remark;

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
